Register-Lib PowerShell.Performance -InitDependencies PowerShell.Logging

function Get-Memory { (Get-Process -ID $PID).WS / 1MB }

function Cleanup-Memory { Measure-Memory { [gc]::Collect() } -verbose }

function Measure-Memory($Process, [switch] $Verbose) {
    $StartingMemory = Get-Memory
    Log-Verbose "Current Memory Usage: $StartingMemory MB" -Verbose:$Verbose
    try {
        if($Process) { 
                . $Process 
        }    
    }
    catch [Exception] {
        $myError = $_
    }
    finally {
        $EndingMemory = (Get-Process -ID $PID).WS / 1MB
        Log-Verbose "Ending Memory: $EndingMemory MB" -Verbose:$Verbose
        $Diff = $EndingMemory - $StartingMemory
        Log "Memory Change: $Diff MB"  
        if($myError){
            throw $myError
        }
    }
    
}


